#include "historymanagerwindow.h"
#include "ui_historymanagerwindow.h"

HistoryManagerWindow::HistoryManagerWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HistoryManagerWindow)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_StyledBackground);
//    qDebug() << parent->width() / 2 << parent->height() / 2;
//    resize(parent->width() / 2, parent->height() / 2);
    // 初始化时加载数据
    updateWindow();

}

HistoryManagerWindow::~HistoryManagerWindow()
{
    delete ui;
}

void HistoryManagerWindow::paintEvent(QPaintEvent *event)
{
    // 调整大小
    QWidget *pa = dynamic_cast<QWidget*>(parent());
    resize(pa->width() / 2, pa->height() / 2);
    QSize s = pa->size();
    move(s.width() / 2 - size().width() / 2, s.height() / 5);
}

void HistoryManagerWindow::updateWindow()
{
    // 绘制界面
    QListWidget *listWidget = this->ui->listWidget;
    listWidget->clear();
    QVector<UrlInfo> infos = HistoryManager::getInstance().getAllHistories();
    for(int i = 0; i < infos.count(); i++){
        QListWidgetItem *widgetItem = new QListWidgetItem();
        UrlInfoItemWidget *item = new UrlInfoItemWidget();
//        connect(item, &UrlInfoItemWidget::deleteItem, this, &HistoryManagerWindow::deleteFavoriteUrlLink);
        item->setUrlInfo(infos[i]);
        listWidget->addItem(widgetItem);
        listWidget->setItemWidget(widgetItem, item);
    }
}

void HistoryManagerWindow::closeEvent(QCloseEvent *event)
{
    // 自定窗口关闭事件
    qDebug() << "自定义窗口关闭事件";
    //    hide();
}

void HistoryManagerWindow::on_pushButton_4_clicked()
{
    hide();
}

